package com.open.source.excel.importto;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.open.source.excel.model.BaseImportModel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author ZonLen since on 2021/9/11 下午4:39
 */
public class ImportAnalysisEventListener<M extends BaseImportModel> extends
    AnalysisEventListener<M> {


  private final Map<Integer, List<M>> importData = new HashMap<>();

  private final AtomicBoolean hasError = new AtomicBoolean(false);

  @Override
  public void invoke(M data, AnalysisContext context) {
    final int sheetNo = context.getCurrentSheet().getSheetNo();
    final List<M> ms = importData.computeIfAbsent(sheetNo, sheetData -> new ArrayList<>());
//    final ValidationEntityResult<M> result = ValidationManager.validation().validateEntity(data);
//    if (result.hasError()){
//      hasError.set(true);
//    }
    ms.add(data);
  }

  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    // 导出错误
    if (hasError.get()){

    // 导入数据库
    }else {

    }
  }
}
